package com.sam.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * @Author: SamZhao
 * @Description:
 * @Date: 2018/01/08 15:14
 */
@Component("user")
//@Service
//@Controller
//@Repository
@Scope(scopeName = "singleton")//指定对象作用范围
public class User {
    @Value("Sam")
    private String name;

    private Integer age;

//    @Autowired  //自动装配
//    @Qualifier("car2")  //使用Qualifier   注解告诉spring容器自动装配那个名称对象
    @Resource(name = "car") //手动注入，指定注入那个名称的对象
    private Car car;

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    @PostConstruct  //在对象被 创建后调用
    public void init(){
        System.out.println("初始化...");
    }

    @PreDestroy //在对象被销毁之前调用
    public void destroy(){
        System.out.println("销毁...");
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", car=" + car +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }
    @Value("18")
    public void setAge(Integer age) {
        this.age = age;
    }

}
